package com.scm.erp.framework.utils;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
/**
 * cpConfig
 * @apiNote 配置c3p0数据源
 * spring.datasource.c3p0.maxPoolSize=20
 * spring.datasource.c3p0.minPoolSize=5
 * spring.datasource.c3p0.initialPoolSize=5
 * # Hibernate 配置
 * spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
 * spring.jpa.database=mysql
 * spring.jpa.show-sql=true
 * spring.jpa.hibernate.ddl-auto=create
 * spring.jpa.open-in-view=false
 * */
@SpringBootConfiguration
@EnableTransactionManagement
public class cpConfig {

    @Bean
    @Qualifier(value ="datasource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.c3p0")
    public DataSource dataSource(){
        DataSourceBuilder<?> sourceBuilder = DataSourceBuilder.create();
        try {
            sourceBuilder.type(com.mchange.v2.c3p0.ComboPooledDataSource.class);
            sourceBuilder.driverClassName("com.mysql.cj.jdbc.Driver");
            sourceBuilder.url("jdbc:mysql://localhost:3306/DBServer?useSSL=false&serverTimezone=UTC");
            sourceBuilder.username("admin");
            sourceBuilder.password("admin123456");
        }catch (Exception e){e.printStackTrace();}
        return sourceBuilder.build();
    }
}
